package com.earl.eatbao.util;

import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;

import javax.imageio.ImageIO;

import org.springframework.web.multipart.MultipartFile;

/**
 * @version:1.0
 * @author: MissYou
 * @date : 16:28
 * @desc:上传文件工具类
 */
public class UpLoadFile {

    /**
     * 本地地址（固化的）
     */
    private static String PATH_NAME = "D:\\java\\workPlace\\eatbao\\src\\main\\webapp";

    /**
     * 保持图片的文件名
     */
    private static String SAVE_FILE_NAME = "imageware";

    /**
     * 文件保存工具类
     * @param file
     * @param fileName
     * @return
     */
    public static String saveFile(MultipartFile file, String fileName) {
        StringBuffer result = new StringBuffer();
        if (null != file && !file.isEmpty()) {
            try{
                String[] fileList = file.getOriginalFilename().split("\\.");
//                String[] fileList = file.getContentType().split("\\");
                //添加防止脚本注入
                if (fileList.length >= 3) {
                    System.out.println("==== 有注入脚本 ====");
                    return null;
                }
                //这里加类型过滤
                BufferedImage bi = ImageIO.read(file.getInputStream());
                if (bi == null ) {
                    System.out.println("====上传图片类型不合法 ====");
                    return null;
                }
                String lastFle = fileList[fileList.length - 1];
                byte[] bytes = file.getBytes();

                //Linux Style
                result.append("/")
                        .append(SAVE_FILE_NAME)
                        .append("/")
                        .append(fileName)
                        .append(".")
                        .append(lastFle);
                //Windows Style
//                result.append("\\")
//                        .append(SAVE_FILE_NAME)
//                        .append("\\")
//                        .append(fileName)
//                        .append(".")
//                        .append(lastFle);
                PATH_NAME = getFilePath();

                String image_path = PATH_NAME  + result.toString();
                System.out.println(image_path);
                BufferedOutputStream outputStream = new BufferedOutputStream(
                        new FileOutputStream(new File(image_path)));
                outputStream.write(bytes);
                outputStream.close();
                return result.toString();
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("==== 文件装字符码出错 ====");
                return null;
            }
        } else {
            System.out.println("==== 文件为空 ====");
            return null;
        }
    }

    /**
     * 获取当前文件路径名
     * @return
     */
    private static String getFilePath() {
        return  System.getProperties().get("user.dir").toString() + "\\src\\main\\webapp\\";
    }

    /**
     * 获取没有“-”的UUID
     * @return
     */
    public static String getUUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }
}
